/**
 * Created by walkman on 2017/6/19.
 */

/***
 wfb-platform AngularJS App Main Script
 ***/
'use strict';

var appDepencies = [
  "ui.router",
  "ui.router.default",
  "ui.bootstrap",
  "oc.lazyLoad",
  'ngRoute',
  "ngSanitize",
  'angular-md5',
  'ngClipboard',
  'bw.paging',
  'WfbPlatformAppApp.Directive',
  'WfbPlatformAppApp.Service'
];

/* WfbPlatform App */
var WfbPlatformApp = angular.module("WfbPlatformApp", appDepencies);

var serverUrl = "";

/* Configure ocLazyLoader(refer: https://github.com/ocombe/ocLazyLoad) */
WfbPlatformApp.config(['$ocLazyLoadProvider', function ($ocLazyLoadProvider) {
  $ocLazyLoadProvider.config({
    // global configs go here
  });
}]);

//AngularJS v1.3.x workaround for old style controller declarition in HTML
WfbPlatformApp.config(['$controllerProvider', function ($controllerProvider) {
  // this option might be handy for migrating old apps, but please don't use it
  // in new ones!
  $controllerProvider.allowGlobals();
}]);

WfbPlatformApp.config(['ngClipProvider', function (ngClipProvider) {
  ngClipProvider.setPath("bower_components/zeroclipboard/dist/ZeroClipboard.swf");
}]);

/********************************************
 END: BREAKING CHANGE in AngularJS v1.3.x:
 *********************************************/
WfbPlatformApp
  .factory('authInterceptor', ['$q', '$window', function ($q, $window) {
    return {
      request: function (config) {
        return config;
      },
      response: function (response) {
        var retCode = response.data.retCode;
        if (!!retCode && retCode == "-104") {
          $window.location.href = '/login.html';
          return false;
        } else if (!!retCode && retCode != "1") {
          var msg = !!response.data.retMsg ? response.data.retMsg : response.retMsg;
          $.gritter.add({
            text: !!msg ? msg : '数据连接失败...',
            time: 2000,
            sticky: false,
            class_name: 'my-sticky-class'
          });
        }
        return response;
      },
      responseError: function (response) {
        var msg = !!response.data.retMsg ? response.data.retMsg : response.retMsg;
        $.gritter.add({
          text: !!msg ? msg : '数据连接失败...',
          time: 2000,
          sticky: false,
          class_name: 'my-sticky-class'
        });
      }
    };
  }])
  .factory('httpInterceptor', ['$q', '$window', function ($q, $window) {

    return {
      request: function (config) {
        config.headers = config.headers || {};
        var token = JSON.parse(window.localStorage.getItem("token")) || "";
        config.headers.Authorization = 'Bearer ' + token.access_token;
        return config;
      },
      response: function (response) {
        return response;
      },
      responseError: function (errorReason) {
        return $q.reject(errorReason);
      },
      requestError: function (errorReason) {
        return $q.reject(errorReason);
      }
    };
  }])
  .factory('httpErrorInterceptor', ['$q', '$rootScope', function ($q, $rootScope) {
    var httpErrorInterceptor = {};

    httpErrorInterceptor.request = function (config) {
      // do something on success
      if (!config.cache) {
        $rootScope.$broadcast('httpRequestSuccess', config);
      }
      return config;
    };

    httpErrorInterceptor.responseError = function (rejection) {
      if (rejection.status === 401) {
        $rootScope.$broadcast('unauthorized', rejection);
      }
      else {
        $rootScope.$broadcast('httpError', rejection);
      }
      return $q.reject(rejection);
    };
    httpErrorInterceptor.requestError = function (rejection) {
      $rootScope.$broadcast('httpError', rejection);
      return $q.reject(rejection);
    };

    return httpErrorInterceptor;
  }])

/* Setup App Main Controller */
WfbPlatformApp.controller('AppController', ['$scope', '$rootScope', function ($scope, $rootScope) {

  $scope.$on('$viewContentLoaded', function () {

  });
}]);

/***
 Layout Partials.
 By default the partials are loaded through AngularJS ng-include directive. In case they loaded in server side(e.g: PHP include function) then below partial
 initialization can be disabled and Layout.init() should be called on page load complete as explained above.
 ***/
/* Setup Layout Part - Header */
WfbPlatformApp.controller('HeaderController', ['$scope', function ($scope) {
  $scope.$on('$includeContentLoaded', function () {
  });
}]);

/* Setup Layout Part - Menu */
WfbPlatformApp.controller('MenuController', ['$scope', function ($scope) {
  $scope.$on('$includeContentLoaded', function () {

  });
}]);

/* Setup Layout Part - Footer */
WfbPlatformApp.controller('FooterController', ['$scope', function ($scope) {
  $scope.$on('$includeContentLoaded', function () {

  });
}]);

/* Setup Rounting For All Pages */
// WfbPlatformApp.config(['$routeProvider', '$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',
//     function ($routeProvider, $stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {

WfbPlatformApp.config(['$stateProvider', '$urlRouterProvider', '$httpProvider', '$locationProvider',
  function ($stateProvider, $urlRouterProvider, $httpProvider, $locationProvider) {

    $locationProvider.html5Mode(false);

    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    if (!$httpProvider.defaults.headers.get) {
      $httpProvider.defaults.headers.get = {};
    }
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

    $httpProvider.defaults.transformRequest = [function (data) {
      //只能是嵌套两层的对象
      // Serialize function
      var param = function (obj) {
        var query = '';
        var name, value, fullSubName, subName, subValue, innerObj, i;

        for (name in obj) {
          value = obj[name];
          if (value instanceof Array) {
            for (i = 0; i < value.length; ++i) {
              subValue = value[i];
              fullSubName = name + '[]';
              innerObj = {};
              innerObj[fullSubName] = subValue;
              query += param(innerObj) + '&';
            }
          } else if (value instanceof Object) {
            for (subName in value) {
              subValue = value[subName];
              fullSubName = subName;
              innerObj = {};
              innerObj[fullSubName] = subValue;
              query += param(innerObj) + '&';
            }
          } else if (value !== undefined && value !== null) {
            query += encodeURIComponent(name) + '='
              + encodeURIComponent(value) + '&';
          }
        }
        return query.length ? query.substr(0, query.length - 1) : query;

      };
      return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];

    // $httpProvider.defaults.transformResponse = function (data) {
    //   return JSON.parse(data);
    // };

    //添加拦截器
    $httpProvider.interceptors.push('httpInterceptor');
    $httpProvider.interceptors.push('httpErrorInterceptor');
    $httpProvider.interceptors.push('authInterceptor');

    // Redirect any unmatched url
    $urlRouterProvider.otherwise("/home.html");

    $stateProvider
    // Dashboard
      .state('home', {
        url: "/home.html",
        templateUrl: "views/home/index.html",
        data: {pageTitle: '首页'},
        controller: "HomeController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/HomeController.js',
                'scripts/services/walletService.js',
              ]
            });
          }]
        }
      })
      //积分商城
      .state('shop', {
        url: "/shop.html",
        templateUrl: "views/shop/index.html",
        data: {pageTitle: '积分商城'},
        controller: "ShopController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                "bower_components/intro.js/introjs.css",
                'bower_components/intro.js/intro.js',
                'scripts/controllers/ShopController.js'
              ]
            });
          }]
        }
      })
      //交易管理 START
      .state('transaction', {
        url: "/transaction",
        templateUrl: "views/transaction/index.html",
        abstract: true,
        default: '.center',
        data: {pageTitle: '交易管理'},
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                "views/transaction/account.css",
              ]
            });
          }]
        }
      })
      //交易中心
      .state('transaction.center', {
        url: "/center",
        templateUrl: "views/transaction/center/index.html",
        abstract: true,
        default: '.investing',
        data: {pageTitle: '交易中心'},
      })
      .state('transaction.center.cart', {
        url: "/cart",
        templateUrl: "views/transaction/center/cart/index.html",
        data: {pageTitle: '订单中心'},
        controller: "CartController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/CartController.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.done', {
        url: "/done",
        templateUrl: "views/transaction/center/done/index.html",
        data: {pageTitle: '已完成'},
        controller: "DoneController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/DoneController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.history', {
        url: "/history",
        templateUrl: "views/transaction/center/history/index.html",
        data: {pageTitle: '分红记录'},
        controller: "HistoryController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/HistoryController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.investing', {
        url: "/investing",
        templateUrl: "views/transaction/center/investing/index.html",
        data: {pageTitle: '订单中心'},
        controller: "InvestingController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/InvestingController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.mailpost', {
        url: "/mailpost",
        templateUrl: "views/transaction/center/mailpost/index.html",
        data: {pageTitle: '地址'},
        controller: "MailpostController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/MailpostController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.order', {
        url: "/order",
        templateUrl: "views/transaction/center/order/index.html",
        data: {pageTitle: '订单中心'},
        controller: "OrderController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/OrderController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.center.rcart', {
        url: "/rcart",
        templateUrl: "views/transaction/center/rcart/index.html",
        data: {pageTitle: '订单中心'},
        controller: "RCartController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before',
              files: [
                'scripts/controllers/RCartController.js'
              ]
            });
          }]
        }
      })
      //增值投资
      .state('transaction.buy', {
        url: "/buy",
        templateUrl: "views/transaction/buy/index.html",
        abstract: true,
        default: '.week',
        data: {pageTitle: '交易管理'}
      })
      .state('transaction.buy.bind', {
        url: "/bind",
        templateUrl: "views/transaction/buy/bind/index.html",
        data: {pageTitle: '绑定交易平台'},
        controller: "BindController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/BindController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.buy.month', {
        url: "/month",
        templateUrl: "views/transaction/buy/month/index.html",
        data: {pageTitle: '月消模式'},
        controller: "MonthController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/MonthController.js',
                'scripts/md5.js'
              ]
            });
          }]
        }
      })
      .state('transaction.buy.recharge', {
        url: "/recharge",
        templateUrl: "views/transaction/buy/recharge/index.html",
        data: {pageTitle: '积分充值'},
        controller: "RechargeController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/services/walletService.js',
                'scripts/services/rechargeService.js',
                'scripts/controllers/RechargeController.js'
              ]
            });
          }]
        }
      })
      .state('transaction.buy.week', {
        url: "/week",
        templateUrl: "views/transaction/buy/week/index.html",
        data: {pageTitle: '期消模式'},
        controller: "WeekController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/WeekController.js',
                'scripts/md5.js'
              ]
            });
          }]
        }
      })
      .state('transaction.buy.withdraw', {
        url: "/withdraw",
        templateUrl: "views/transaction/buy/withdraw/index.html",
        data: {pageTitle: '积分提现'},
        controller: "WithdrawController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/WithdrawController.js',
                'scripts/services/transactionService.js',
                'scripts/md5.js'
              ]
            });
          }]
        }
      })
      //积分明细
      .state('transaction.score', {
        url: "/score",
        templateUrl: "views/transaction/score/index.html",
        abstract: true,
        default: '.original',
        data: {pageTitle: '积分明细'}
      })
      .state('transaction.score.original', {
        url: "/original",
        templateUrl: "views/transaction/score/original/index.html",
        data: {pageTitle: '原始积分'},
        controller: "OriginalController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/OriginalController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.capital', {
        url: "/capital",
        templateUrl: "views/transaction/score/capital/index.html",
        data: {pageTitle: '资本积分'},
        controller: "CapitalController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/CapitalController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.mode', {
        url: "/mode",
        templateUrl: "views/transaction/score/mode/index.html",
        data: {pageTitle: '模态积分'},
        controller: "ModeController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/ModeController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.dynamic', {
        url: "/dynamic",
        templateUrl: "views/transaction/score/dynamic/index.html",
        data: {pageTitle: '动态积分'},
        controller: "DynamicController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/DynamicController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.static', {
        url: "/static",
        templateUrl: "views/transaction/score/static/index.html",
        data: {pageTitle: '静态积分'},
        controller: "StaticController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/StaticController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.electronic', {
        url: "/electronic",
        templateUrl: "views/transaction/score/electronic/index.html",
        data: {pageTitle: '电子积分'},
        controller: "ElectronicController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/ElectronicController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.recart', {
        url: "/recart",
        templateUrl: "views/transaction/score/recart/index.html",
        data: {pageTitle: '复消积分'},
        controller: "RecartController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/RecartController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.consume', {
        url: "/consume",
        templateUrl: "views/transaction/score/consume/index.html",
        data: {pageTitle: '复消积分'},
        controller: "ConsumeController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/ConsumeController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      .state('transaction.score.active', {
        url: "/active",
        templateUrl: "views/transaction/score/active/index.html",
        data: {pageTitle: '激活积分'},
        controller: "ActiveController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/ActiveController.js',
                'scripts/services/transactionService.js'
              ]
            });
          }]
        }
      })
      //账户管理
      .state('account', {
        url: "/account",
        templateUrl: "views/account/index.html",
        abstract: true,
        default: '.dashboard',
        data: {pageTitle: '账户管理'},
        //controller: "AccountController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                "styles/account.css",
              ]
            });
          }]
        }
      })
      .state('account.dashboard', {
        url: "/dashboard",
        templateUrl: "views/account/dashboard/index.html",
        data: {pageTitle: '账户首页'},
        controller: "DashboardController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/DashboardController.js',
                'scripts/services/dashboardService.js'
              ]
            });
          }]
        }
      })
      //我的团队
      .state('account.team', {
        url: "/team",
        templateUrl: "views/account/team/index.html",
        abstract: true,
        default: '.member',
        data: {pageTitle: '我的团队'}
      })
      .state('account.team.active', {
        url: "/active",
        templateUrl: "views/account/team/active/index.html",
        data: {pageTitle: '我的团队'},
        controller: "TeamActiveController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TeamActiveController.js',
                'scripts/services/teamService.js'
              ]
            });
          }]
        }
      })
      .state('account.team.award', {
        url: "/award",
        templateUrl: "views/account/team/award/index.html",
        data: {pageTitle: '我的团队'},
        controller: "TeamAwardController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TeamAwardController.js',
                'scripts/services/teamService.js',
                'bower_components/jquery.zeroclipboard/dist/jquery.zeroclipboard.js'
              ]
            });
          }]
        }
      })
      .state('account.team.level', {
        url: "/level",
        templateUrl: "views/account/team/level/index.html",
        data: {pageTitle: '我的团队'},
        controller: "TeamLevelController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TeamLevelController.js',
                'scripts/services/teamService.js'
              ]
            });
          }]
        }
      })
      .state('account.team.member', {
        url: "/member",
        templateUrl: "views/account/team/member/index.html",
        data: {pageTitle: '我的团队'},
        controller: "TeamMemberController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TeamMemberController.js',
                'scripts/services/teamService.js'
              ]
            });
          }]
        }
      })
      .state('account.team.record', {
        url: "/record",
        templateUrl: "views/account/team/record/index.html",

        data: {pageTitle: '我的团队'},
        controller: "TeamRecordController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TeamRecordController.js',
                'scripts/services/teamService.js'
              ]
            });
          }]
        }
      })
      .state('account.giro', {
        url: "/giro",
        templateUrl: "views/account/giro/index.html",
        abstract: true,
        default: '.transfer',
        data: {pageTitle: '钱包转账 '}
      })
      // .state('account.giro.exchange', {
      //   url: "/exchange",
      //   templateUrl: "views/account/giro/exchange/index.html",
      //   data: {pageTitle: '积分兑换'},
      //   controller: "ExchangeController",
      //   resolve: {
      //     deps: ['$ocLazyLoad', function ($ocLazyLoad) {
      //       return $ocLazyLoad.load({
      //         name: 'WfbPlatformApp',
      //         insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
      //         files: [
      //           'scripts/controllers/ExchangeController.js',
      //           'scripts/md5.js'
      //         ]
      //       });
      //     }]
      //   }
      // })
      .state('account.giro.transfer', {
        url: "/transfer",
        templateUrl: "views/account/giro/transfer/index.html",
        data: {pageTitle: '积分转账 '},
        controller: "TransferController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/TransferController.js',
                'scripts/md5.js'
              ]
            });
          }]
        }
      })
      .state('account.profile', {
        url: "/profile",
        templateUrl: "views/account/profile/index.html",
        data: {pageTitle: '个人资料'},
        controller: "ProfileController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/ProfileController.js',
                'scripts/services/profileService.js'
              ]
            });
          }]
        }
      })
      .state('help', {
        url: "/help",
        templateUrl: "views/help/index.html",
        abstract: true,
        default: '.about',
        data: {pageTitle: '帮助'}
      })
      .state('help.about', {
        url: "/about",
        templateUrl: "views/help/about/index.html",
        data: {pageTitle: '关于'},
        controller: "AboutController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/AboutController.js'
              ]
            });
          }]
        }
      })
      .state('help.note', {
        url: "/note",
        templateUrl: "views/help/note/index.html",
        data: {pageTitle: '公告'},
        controller: "NoteController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/NoteController.js'
              ]
            });
          }]
        }
      })
      .state('help.faq', {
        url: "/faq",
        templateUrl: "views/help/faq/index.html",
        data: {pageTitle: '常见问题'},
        controller: "FaqController",
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load({
              name: 'WfbPlatformApp',
              insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
              files: [
                'scripts/controllers/FaqController.js'
              ]
            });
          }]
        }
      });
  }]);

/* Init global settings and run the app */
WfbPlatformApp.run(['$rootScope', '$location', 'loginService', '$http', 'localService', '$window',

  function ($rootScope, $location, loginService, $http, localService, $window) {

    $rootScope.$on('$routeChangeStart', function ($event) {
      $rootScope.childMenu = $location.$$path;
    });

    // loginService.getMenuList({}).then(function (response) {
    //     $rootScope.menuList = response;
    //     $rootScope.adminName = sessionStorage.getItem("userName");
    // });

    $rootScope.logout = function ($event) {
      loginService.logout().then(function () {
        $window.location.href = '/login.html';
      })
    };

  }]);

